//
//  NeaView.m
//  HappyTrip
//
//  Created by GeminyXC on 15-4-4.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NeaView.h"



//#import "TestViewController.h"


//#import "TestViewController.h"

//#import "TestViewController.h"



#import "NearbyDetailViewController.h"




#import "HappyDatabase.h"




@implementation NeaView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    [_happyArr release];
    [_happyObjInfo release];

    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blueColor];
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self addSubview:_tableView];
        [_tableView release];
        
        //在初始化的时候建立一个重用池
        [self.tableView  registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
        
    }
    return self;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == self.happyArr.count) {
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        self.tableView.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"collect.png"]] autorelease];
    }else
    {
        self.tableView.backgroundView = nil;
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    }

    self.happyArr = [NSMutableArray array];
    HappyDatabase *happyDB = [HappyDatabase shareInstance];
    self.happyArr = [happyDB selectAll];
    
    return self.happyArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];

    cell.textLabel.text = [[self.happyArr objectAtIndex:indexPath.row] theName];
    cell.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
    NSLog(@"这里打印收藏的名字:%@", cell.textLabel.text);
    return cell;
}






//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    TestViewController *testVC = [[TestViewController alloc] init];
//    [self.target pushViewController:testVC animated:YES];
//    [testVC release];
//}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearbyDetailViewController *nearbyVC = [[NearbyDetailViewController alloc] init];
   
    self.happyObjInfo = [self.happyArr objectAtIndex:indexPath.row];
    nearbyVC.type = self.happyObjInfo.theType;
    nearbyVC.urlID = self.happyObjInfo.theUrlId;
    NSLog(@"打印type和ID%@%@", nearbyVC.type, nearbyVC.urlID);
    [self.target pushViewController:nearbyVC animated:YES];
    [nearbyVC release];
}




@end
